Option Strict On
Option Explicit On

Imports CObject
Imports Objects.Utility
Imports System.IO
Imports System.Net.Mail

Public Class Email
    Inherits CObject.CObject

    Private mdrDynamicData As DataRow
    Private _msReportingEmail As String = String.Empty
    Private _msFormID As String = String.Empty

    Public Overrides Function Clone() As CObject.CObject
        Return New Email
    End Function

    Public Overrides ReadOnly Property Key() As String
        Get
            Return "EmailInfoID"
        End Get
    End Property

    Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
        Select Case peCommandType
            Case CommandType.SQLSelect
                Return "SELECT * FROM vwEmail"
            Case Else
                Throw New SystemException("Invalid Command Type")
        End Select
	End Function

	Public Sub New()
        MyBase.New()
        Dim loSetting As Settings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        _msReportingEmail = loSetting("ReportingEmail").ToString
    End Sub

    Public Sub New(ByVal piEMailID As Integer)
        MyBase.New(piEMailID)
        Dim loSetting As Settings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        _msReportingEmail = loSetting("ReportingEmail").ToString
    End Sub

    Public Sub New(ByVal piEmailId As Integer, ByVal pdrDynamicData As DataRow)
        MyBase.New(piEmailId)
        mdrDynamicData = pdrDynamicData
        Dim loSetting As Settings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        _msReportingEmail = loSetting("ReportingEmail").ToString
    End Sub

    Public ReadOnly Property ReportingEmailAddress() As String
        Get
            Return _msReportingEmail
        End Get
    End Property

    Public ReadOnly Property EmailInfoID() As Integer
        Get
            Return casttointeger(getproperty("EmailInfoID"))
        End Get
    End Property

    Public Property FormID() As String
        Get
            Return _msFormID
        End Get
        Set(ByVal value As String)
            _msFormID = value
        End Set
    End Property

    Public Property FromEmailID() As String
        Get
            Return CastToString(GetProperty("FromEmailID"))
        End Get
        Set(ByVal value As String)
            SetProperty("FromEmailID", value)
        End Set
    End Property

    Public Property Subject() As String
        Get
            Return CastToString(GetProperty("Subject"))
        End Get
        Set(ByVal Value As String)
            SetProperty("Subject", Value)
        End Set
    End Property

    Public ReadOnly Property EmailTag() As String
        Get
            Return CastToString(GetProperty("EmailTag"))
        End Get
    End Property

    Public Property BodyText() As String
        Get
            Dim loUtility As New Utility.UtilityFunctions
            If Not isDynamic Then
                Return casttostring(getproperty("BodyText"))
            Else
                Return loUtility.BuildDynamicBody(casttostring(getproperty("BodyText")), Nothing, mdrDynamicData, "{", "}")
            End If
        End Get
        Set(ByVal Value As String)
            setproperty("BodyText", Value)
        End Set
    End Property

    Public ReadOnly Property ReminderText() As String
        Get
            Return casttostring(getproperty("ReminderText"))
        End Get
    End Property

    Public ReadOnly Property isDynamic() As Boolean
        Get
            If Properties.ContainsKey("isDynamic") Then
                Return CBool(CastToString(GetProperty("isDynamic")))
            Else
                Return False
            End If
        End Get
    End Property

    Public Sub SendEmail(ByVal psEmailAddress As String)
        SendMail(psEmailAddress, FromEmailID, Subject, BodyText)
    End Sub

    Public Sub SendEmail(ByVal psTo As String, ByVal psCC As String)
        SendMail(psTo, FromEmailID, Subject, BodyText, psCC)
    End Sub

    Public Sub SendEmail(ByVal psTo As String, ByVal psAttachmentFileName As String, ByVal psCC As String, ByVal psBCC As String)
        SendMail(psTo, FromEmailID, Subject, BodyText, psAttachmentFileName, psCC, psBCC)
    End Sub

    Public Sub SendEmail(ByVal psTo As String, ByVal psAttachmentFileName() As String, ByVal psCC As String, ByVal psBCC As String)
        SendMail(psTo, FromEmailID, Subject, BodyText, psAttachmentFileName, psCC, psBCC)
    End Sub

    Private Sub SendMail(ByVal pstrTo As String, ByVal pstrFrom As String, ByVal pstrSubject As String, ByVal pstrBody As String, ByVal psAttachmentFileName() As String, ByVal pstrCC As String, ByVal pstrBCC As String)
        Dim lbInvalid As Boolean = CheckForInvalid(pstrTo)
        pstrBody += GetEmailTag()
        If lbInvalid Then
            pstrTo = ReportingEmailAddress
            pstrSubject = pstrSubject + ": Invalid To Address"
        End If

        Dim loSettings As Settings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        If Not loSettings("EmailToOverride") Is Nothing AndAlso loSettings("EmailToOverride").Length > 0 Then
            pstrBody += "<p>The recipient of this email was overridden:   Actual recipients were...<br>"
            pstrBody += pstrTo
            pstrBody += "<p>CC:" & pstrCC
            pstrCC = String.Empty
            pstrTo = loSettings("EmailToOverride")
        End If
        Dim loMailMessage As MailMessage = New MailMessage(pstrFrom, pstrTo, pstrSubject, pstrBody)

        With loMailMessage
            If Not pstrCC Is Nothing AndAlso pstrCC <> "" Then
                .CC.Add(pstrCC)
            End If

            If Not pstrBCC Is Nothing AndAlso pstrBCC <> "" Then
                .Bcc.Add(pstrBCC)
            End If
            If Not lbInvalid Then
                .Bcc.Add(ReportingEmailAddress)
            End If
            Dim lsAttachmentName As String = String.Empty
            For Each lsAttachmentName In psAttachmentFileName
                If Not lsAttachmentName Is Nothing AndAlso lsAttachmentName <> "" Then
                    .Attachments.Add(New Attachment(lsAttachmentName))
                End If
            Next
        End With
        loMailMessage.IsBodyHtml = True
        Dim loServer As SmtpClient = New SmtpClient(GetSMTPServer())
        Try
            loServer.Send(loMailMessage)
        Catch e As System.Net.Mail.SmtpException
        End Try

        loMailMessage.Attachments.Dispose()
    End Sub

    Private Sub SendMail(ByVal pstrTo As String, ByVal pstrFrom As String, ByVal pstrSubject As String, ByVal pstrBody As String, ByVal psAttachmentFileName As String, ByVal pstrCC As String, ByVal pstrBCC As String)
        Dim lbInvalid As Boolean = CheckForInvalid(pstrTo)
        pstrBody += GetEmailTag()
        If lbInvalid Then
            pstrTo = ReportingEmailAddress
            pstrSubject = pstrSubject + ": Invalid To Address"
        End If
        Dim loSettings As Settings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        If Not loSettings("EmailToOverride") Is Nothing AndAlso loSettings("EmailToOverride").Length > 0 Then
            pstrBody += "<p>The recipient of this email was overridden:   Actual recipients were...<br>"
            pstrBody += pstrTo
            pstrBody += "<p>CC:" & pstrCC
            pstrCC = String.Empty
            pstrTo = loSettings("EmailToOverride")
            pstrBCC = String.Empty

        End If
        Dim loMailMessage As MailMessage = New MailMessage(pstrFrom, pstrTo, pstrSubject, pstrBody)

        With loMailMessage
            If Not pstrCC Is Nothing AndAlso pstrCC <> "" Then
                .CC.Add(pstrCC)
            End If

            If Not pstrBCC Is Nothing AndAlso pstrBCC <> "" Then
                .Bcc.Add(pstrBCC)
            End If
            If Not lbInvalid Then
                .Bcc.Add(ReportingEmailAddress)
            End If
            If Not psAttachmentFileName Is Nothing AndAlso psAttachmentFileName <> "" Then
                .Attachments.Add(New Attachment(psAttachmentFileName))
            End If
        End With
        loMailMessage.IsBodyHtml = True
        Dim loServer As SmtpClient = New SmtpClient(GetSMTPServer())
        Try
            loServer.Send(loMailMessage)
        Catch e As System.Net.Mail.SmtpException
        End Try

        loMailMessage.Attachments.Dispose()
    End Sub

    Private Sub SendMail(ByVal pstrTo As String, ByVal pstrFrom As String, ByVal pstrSubject As String, ByVal pstrBody As String, ByVal psAttachmentFileName As String, ByVal pstrCC As String)

        pstrBody += GetEmailTag()
        Dim lbInvalid As Boolean = CheckForInvalid(pstrTo)
        If lbInvalid Then
            pstrTo = ReportingEmailAddress
            pstrSubject = pstrSubject + ": Invalid To Address"
        End If
        Dim loSettings As Settings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        If Not loSettings("EmailToOverride") Is Nothing AndAlso loSettings("EmailToOverride").Length > 0 Then
            pstrBody += "<p>The recipient of this email was overridden:   Actual recipients were...<br>"
            pstrBody += pstrTo
            pstrBody += "<p>CC:" & pstrCC
            pstrCC = String.Empty
            pstrTo = loSettings("EmailToOverride")
        End If

        Dim loMailMessage As MailMessage = New MailMessage(pstrFrom, pstrTo, pstrSubject, pstrBody)

        With loMailMessage
            If Not pstrCC Is Nothing AndAlso pstrCC <> "" Then
                .CC.Add(pstrCC)
            End If
            If Not lbInvalid Then
                .Bcc.Add(ReportingEmailAddress)
            End If
            If Not psAttachmentFileName Is Nothing AndAlso psAttachmentFileName <> "" Then
                .Attachments.Add(New Attachment(psAttachmentFileName))
            End If
        End With
        loMailMessage.IsBodyHtml = True
        Dim loServer As SmtpClient = New SmtpClient(GetSMTPServer())
        Try
            loServer.Send(loMailMessage)
        Catch e As System.Net.Mail.SmtpException
        End Try

        loMailMessage.Attachments.Dispose()
    End Sub

    Private Sub SendMail(ByVal pstrTo As String, ByVal pstrFrom As String, ByVal pstrSubject As String, ByVal pstrBody As String, Optional ByVal pstrCC As String = "")
        pstrBody += GetEmailTag()
        Dim lbInvalid As Boolean = CheckForInvalid(pstrTo)
        If lbInvalid Then
            pstrTo = ReportingEmailAddress
            pstrSubject = pstrSubject + ": Invalid To Address"
        End If
        Dim loSettings As Settings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        If Not loSettings("EmailToOverride") Is Nothing AndAlso loSettings("EmailToOverride").Length > 0 Then
            pstrBody += "<p>The recipient of this email was overridden:   Actual recipients were...<br>"
            pstrBody += pstrTo
            pstrBody += "<p>CC:" & pstrCC
            pstrCC = String.Empty
            pstrTo = loSettings("EmailToOverride")
        End If
       

        Dim loMailMessage As MailMessage = New MailMessage(pstrFrom, pstrTo, pstrSubject, pstrBody)
        If pstrCC.Trim.Length > 0 Then
            loMailMessage.CC.Add(pstrCC)
        End If
        If Not lbInvalid Then
            loMailMessage.Bcc.Add(ReportingEmailAddress)
        End If
        loMailMessage.IsBodyHtml = True
        Dim loServer As SmtpClient = New SmtpClient(GetSMTPServer())

        Try
            loServer.Send(loMailMessage)
        Catch ex As System.Net.Mail.SmtpException
        End Try
    End Sub

    Private Function CheckForInvalid(ByVal pstrTo As String) As Boolean

        Dim Settings As New Settings()
        If pstrTo Is Nothing OrElse pstrTo.Length = 0 Then
            pstrTo = ReportingEmailAddress

            SendMail(Settings("ErrorAdmin").ToString, pstrTo, "Invalid Email Detected", "An attempt to send an email to a user with no To Address has been detected.  This email is in the Reporting Mailbox")
            Return True
        End If
        Return False
    End Function

    Private Function GetSMTPServer() As String
        Dim loSettings As New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        Return loSettings("smtpserver")

    End Function

    Private Function GetEmailTag() As String
        Dim loSettings As Settings
        If _msFormID.Length > 0 Then
            loSettings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT, _msFormID)
        Else
            loSettings = New Settings(ConnectionServer.Connection.SourceDataBase.CMT)
        End If

        Dim lsTag As String = "<p>***Please do not reply to this email.</p>"

        If EmailTag <> "" Then
            Return EmailTag.Trim()
        Else
            If loSettings("emailtag") Is Nothing OrElse loSettings("emailtag").Length = 0 Then
                Return lsTag
            Else
                Return loSettings("emailtag")
            End If
        End If

    End Function

    Public Overrides ReadOnly Property SourceDataBase() As ConnectionServer.Connection.SourceDataBase
        Get
            Return ConnectionServer.Connection.SourceDataBase.CMT
        End Get
    End Property

End Class
